import { getRouters as _getRouters } from '@/api/role'

export const useRouters = () => {
  const getRouters = async () => {
    const meuns = await _getRouters()
    const routerList = meuns
    formatRouterPath(routerList, '')
    formatRouter(routerList)
    return routerList
  }

  const formatRouter = (menus: any[]) => {
    menus.forEach((i) => {
      // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
      if (i.children && i.children.length > 0) {
        let flag = false
        i.children.forEach((o: { menuType: string }) => {
          if (o.menuType === 'C') {
            flag = true
          }
        })
        if (flag && i.path !== '/jobCenter/jobManage') {
          i.hasChildren = true
          formatRouter(i.children)
        } else {
          i.hasChildren = false
        }
      } else {
        i.hasChildren = false
      }
    })
  }

  const formatRouterPath = (menus: any[], path: string) => {
    menus.forEach((i) => {
      // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
      i.path = `${path}/${i.path}`
      if (i.children.length > 0) {
        formatRouterPath(i.children, i.path)
      }
    })
  }
  return {
    getRouters
  }
}
